package com.aso114.project.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.base.BaseDialog;
import com.aso114.project.bean.PriceListBean;
import com.aso114.project.mvp.activity.OpenVipActivity;
import com.aso114.project.util.ScreenTools;

import java.util.ArrayList;
import java.util.List;

public class PayDialog extends BaseDialog implements View.OnClickListener {
    private Context mContext;
    private OnDialogClick onDialogClick;
    private List<PriceListBean> dialogBean = new ArrayList<>();
    private PriceListBean zfbBean = new PriceListBean();
    private PriceListBean wxBean = new PriceListBean();

    public PayDialog(Context context, List<PriceListBean> dialogBean, OnDialogClick onDialogClick) {
        super(context);
        this.mContext = context;
        this.onDialogClick = onDialogClick;
        this.dialogBean = dialogBean;
        init();
    }


    private void init() {
        View contentView = LayoutInflater.from(mContext).inflate(R.layout.pay_dialog, null);
        setContentView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        Window window = getWindow();
        // 设置显示动画
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y = ScreenTools.getScreenHeight(mContext);
        // 以下这两句是为了保证按钮可以水平满屏
        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wl.height = ViewGroup.LayoutParams.MATCH_PARENT;
        // 设置显示位置
        this.onWindowAttributesChanged(wl);
        this.setCancelable(true);
        this.setCanceledOnTouchOutside(false);
        for (int i=0;i<dialogBean.size();i++){
            if ("1".equals(dialogBean.get(i).getVipType())){
                // 支付宝
                zfbBean = dialogBean.get(i);
            }else if ("2".equals(dialogBean.get(i).getVipType())){
                // 微信
                wxBean = dialogBean.get(i);
            }
        }
        TextView zfbPrice = findViewById(R.id.pay_dialog_zfb_price);
        TextView zfbTv = findViewById(R.id.pay_dialog_zfb_tv);
        TextView wxPrice = findViewById(R.id.pay_dialog_wx_price);
        TextView wxTv = findViewById(R.id.pay_dialog_wx_tv);
        contentView.findViewById(R.id.pay_dialog_close).setOnClickListener(this);
        contentView.findViewById(R.id.pay_dialog_zfb).setOnClickListener(this);
        contentView.findViewById(R.id.pay_dialog_wx).setOnClickListener(this);
        Drawable drawable = mContext.getResources().getDrawable(R.mipmap.kthy_ic_yh);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        if (wxBean.getVipPrice()<zfbBean.getVipPrice()){
            // 微信vip价格小于支付宝（支付宝显示优惠）
            wxTv.setCompoundDrawables(null, null, drawable, null);  //设置到控件的右面
            zfbTv.setCompoundDrawables(null, null, null, null);  //设置到控件的右面
        }else {
            zfbTv.setCompoundDrawables(null, null, drawable, null);  //设置到控件的右面
            wxTv.setCompoundDrawables(null, null, null, null);  //设置到控件的右面
        }
        zfbPrice.setText("￥" + String.format("%.2f", zfbBean.getVipPrice()));
        wxPrice.setText("￥" + String.format("%.2f", wxBean.getVipPrice()));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.pay_dialog_close:
                dismiss();
                break;
            case R.id.pay_dialog_zfb:
                if (onDialogClick != null)
                    onDialogClick.onZfb(zfbBean);
                dismiss();
                break;
            case R.id.pay_dialog_wx:
                if (onDialogClick != null)
                    onDialogClick.onWx(wxBean);
                dismiss();
                break;
        }
    }

    public interface OnDialogClick {
        void onZfb(PriceListBean bean);

        void onWx(PriceListBean bean);
    }
}
